package com.estun.backroundManagementService.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estun.backroundManagementService.config.factoryMes.ApiCaller;
import com.estun.backroundManagementService.config.properties.ApiInfoProperties;
import com.estun.backroundManagementService.config.properties.LineInfoProperties;
import com.estun.backroundManagementService.domain.factoryMes.request.PushAlarmRequest;
import com.estun.backroundManagementService.domain.factoryMes.request.base.BaseMesRequest;
import com.estun.backroundManagementService.domain.factoryMes.request.base.BaseMesResponse;
import com.estun.backroundManagementService.modules.alarm.model.StationAlarm;
import com.estun.backroundManagementService.modules.alarm.model.StationAlarmMapping;
import com.estun.backroundManagementService.modules.alarm.service.StationAlarmService;
import com.estun.backroundManagementService.modules.position.service.DeviceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yusiocng
 * @date 2023/12/19
 * @description 推送定时任务
 */

@Slf4j
@Component
@AllArgsConstructor
public class PushDeviceAlarmTask {

    private final LineInfoProperties lineInfoProperties;
    private final ApiCaller apiCaller;
    private final ApiInfoProperties apiInfoProperties;
    private final StationAlarmService stationAlarmService;
    private final DeviceService deviceService;


    /**
     * 推送 告警
     */
//    @Scheduled(fixedRate = 5 * 60 * 1000)
    public void pushStationAlarm() {
        log.info("【推送告警信息】开始任务");
        long beginTime = System.currentTimeMillis();
        List<StationAlarm> toBePushList = stationAlarmService.list(new QueryWrapper<StationAlarm>()
                .ne("submit_status", 1)
                .ne("alarm_index", 1997));
        if (CollectionUtils.isEmpty(toBePushList)) {
            log.info("【推送告警信息】,本次没有待推送告警，任务结束");
            return;
        }
        int toBePushSize = toBePushList.size();
        int failNumber = 0;
        for (int i = 0; i < toBePushList.size(); i++) {
            StationAlarm stationAlarm = toBePushList.get(i);
            log.info("【推送告警信息】本次任务数量:{},当前第{}个", toBePushSize, i + 1);
            PushAlarmRequest passStationRequest = buildPushAlarmData(stationAlarm);
            BaseMesRequest<PushAlarmRequest> mesRequest = new BaseMesRequest<>();
            mesRequest.setData(Collections.singletonList(passStationRequest));
            BaseMesResponse mesResponse = apiCaller.post(apiInfoProperties.getPushWarning(), mesRequest, BaseMesResponse.class);
            //更新推送状态
            if (mesResponse.isSuccess()) {
                stationAlarm.setSubmitStatus(1);
            } else {
                log.error("【推送告警信息】推送失败 工厂MES返回:{}", mesResponse);
                stationAlarm.setSubmitStatus(-1);
                failNumber++;
            }
            stationAlarm.setSubmitNumber(stationAlarm.getSubmitNumber() + 1);
            stationAlarmService.updateById(stationAlarm);
        }
        log.info("【推送告警信息】 本次任务数量：{},成功数量:{},失败数量:{},耗时:{}毫秒",
                toBePushSize, toBePushSize - failNumber, failNumber, System.currentTimeMillis() - beginTime);
    }

    /**
     * 构造告警信息requestBody
     */
    private PushAlarmRequest buildPushAlarmData(StationAlarm stationAlarm) {
        PushAlarmRequest pushAlarmRequest = new PushAlarmRequest();
        pushAlarmRequest.setLineCode(lineInfoProperties.getLineCode());
        StationAlarmMapping stationCodeByAlarmId = stationAlarmService.getStationCodeByAlarmIndex(stationAlarm.getAlarmIndex());
        pushAlarmRequest.setStationCode(stationCodeByAlarmId.getOpCode());
        String deviceCode = deviceService.getDeviceCodeByStationId(stationCodeByAlarmId.getOpCode());
        pushAlarmRequest.setMachineCode(deviceCode);
        pushAlarmRequest.setAcquisitCode(deviceCode);
        pushAlarmRequest.setWarningType("0");
        pushAlarmRequest.setWarningTime(stationAlarm.getUpdateTime());
        pushAlarmRequest.setWarningDesc(stationCodeByAlarmId.getContent());
        return pushAlarmRequest;
    }



}
